package org.llc.oauthclient.service.impl;

import org.llc.common.model.Result;
import org.llc.oauthclient.client.ReqGetTokenByAuthorizationCodeDTO;
import org.llc.oauthclient.client.UserOauthClient;
import org.llc.oauthclient.service.CodeService;
import org.springframework.stereotype.Service;

/**
 * 授权码获取Token ServiceImpl
 *
 * @author llc
 * @date 2020/5/7 16:20
 * @since 1.0.0
 */
@Service
public class CodeServiceImpl implements CodeService {

    /**
     * 用户认证服务
     */
    private final UserOauthClient userOauthClient;

    public CodeServiceImpl(UserOauthClient userOauthClient) {
        this.userOauthClient = userOauthClient;
    }

    /**
     * 调用用户认证服务授权码模式获取Token
     * {@inheritDoc}
     * @author llc
     * @date 2020/5/7 16:42
     */
    @Override
    public Result getTokenByClientAuthorizationCode(String code) {
        ReqGetTokenByAuthorizationCodeDTO reqGetTokenByAuthorizationCodeDto = new ReqGetTokenByAuthorizationCodeDTO();
        reqGetTokenByAuthorizationCodeDto.setCode(code);
        reqGetTokenByAuthorizationCodeDto.setRedirectUri("http://localhost:9064/resource/client/authorize/code");
        reqGetTokenByAuthorizationCodeDto.setClientId("clientId");
        reqGetTokenByAuthorizationCodeDto.setClientSecret("secret");
        Result result = userOauthClient.getTokenByClientAuthorizationCode(reqGetTokenByAuthorizationCodeDto);
        if(result.isSuccess()){
            // 响应结果JSON
            //TODO 拿token调用用户信息服务（还没开发）查询用户详情
        }
        return result;
    }
}
